//
//  ModifyPasswordViewController.m
//  Newproject3
//
//  Created by rimi on 16/9/13.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "HUser.h"


@interface ModifyPasswordViewController (){
    
}
@property (nonatomic, strong) UITextField *usernames;
@property (nonatomic, strong) UITextField *password;
@property (nonatomic,strong)UITextField *surePassword;
@property (nonatomic, strong) UIButton *loginOfButton;
@property (nonatomic,strong)UITextField *name;
@property (nonatomic,strong)NSMutableArray *dataArr;


@end

@implementation ModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.centerLabel.text = @"密码更改";
    [self.leftButton setImage:[UIImage imageNamed:@"个人中心-left@2x"] forState:UIControlStateNormal];

    [self initUi];
}

-(void)initUi{
    [self.view addSubview:self.usernames];
    [self.view addSubview:self.password];
    [self.view addSubview:self.surePassword];
    [self.view addSubview:self.name];
    [self.view addSubview:self.loginOfButton];
}

// 隐藏下面的分栏标签，进入这个界面的时候隐藏，离开界面的时候显示
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

#pragma mark -- 点击事件

-(void)loginOfButton_action{
    
    if ([HUser currentUser].isLogin == YES) {
       
        
        __block NSDictionary * dicU = nil;
        [self.dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([self.usernames.text isEqualToString:obj[@"username"]]) {
                
                dicU = obj;
                // *stop = YES 是跳出循环遍历的方法
                *stop = YES;
            }
        }];
        
        if (dicU) {
            [self.dataArr removeObject:dicU];
        }
        
         NSDictionary *dict = @{@"username":self.usernames.text,@"password":self.password.text,@"nicheng":self.name.text};
        
        [self.dataArr addObject:dict];
        
        [[NSUserDefaults standardUserDefaults] setObject:self.dataArr forKey:@"userInfo"];
        
    }
    else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alertController addAction:photoAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }

}
// 继承父类的方法
-(void)action_LeftButtonClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - getter
- (UITextField *)usernames{
    if (!_usernames) {
        _usernames = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.015, khieght * 0.15, kwidth * 0.95, khieght * 0.07)];
        _usernames.placeholder = @"帐号";
        _usernames.borderStyle = UITextBorderStyleRoundedRect;
        _usernames.clearButtonMode = UITextFieldViewModeWhileEditing;
        _usernames.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth * 0.1, khieght * 0.05)];
        image.image = [UIImage imageNamed:@"zhanghu"];
        _usernames.leftView = image;
    }
    return _usernames;
}

- (UITextField *)password{
    if (!_password) {
        _password = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.015, khieght * 0.23, kwidth * 0.95, khieght * 0.07)];
        _password.placeholder = @"请输入密码";
        _password.secureTextEntry = YES;
        _password.borderStyle = UITextBorderStyleRoundedRect;
        _password.clearButtonMode = UITextFieldViewModeWhileEditing;
        _password.secureTextEntry = YES;
        // uitextfield 左侧图像在什么时候存在
        _password.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth * 0.1, khieght * 0.05)];
        imageView.image = [UIImage imageNamed:@"mima"];
        _password.leftView = imageView;
    }
    return _password;
}

-(UITextField *)surePassword{
    if (_surePassword == nil) {
        _surePassword = [[UITextField alloc]initWithFrame:CGRectMake(kwidth*0.015, khieght*0.31, kwidth*0.95, khieght*0.07)];
        _surePassword.placeholder = @"请再次输入密码";
        _surePassword.secureTextEntry = YES;
        _surePassword.borderStyle = UITextBorderStyleRoundedRect;
        _surePassword.clearButtonMode = UITextFieldViewModeWhileEditing;
        _surePassword.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth * 0.1, khieght * 0.05)];
        imageView.image = [UIImage imageNamed:@"mima"];
        _surePassword.leftView = imageView;
        
    }
    return _surePassword;
}

- (UIButton *)loginOfButton{
    if (!_loginOfButton) {
        _loginOfButton = [[UIButton alloc] initWithFrame:CGRectMake(kwidth * 0.015, khieght * 0.51, kwidth * 0.95, khieght * 0.07)];
        _loginOfButton.layer.cornerRadius = 8;
        _loginOfButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.467 blue:0.949 alpha:1.000];
        [_loginOfButton setTitle:@"确认" forState:UIControlStateNormal];
        [_loginOfButton addTarget:self action:@selector(loginOfButton_action) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginOfButton;
}

-(UITextField *)name{
    if (_name == nil) {
        _name = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.015, khieght * 0.39, kwidth * 0.95, khieght * 0.07)];
        _name.borderStyle = UITextBorderStyleRoundedRect;
        _name.clearButtonMode = UITextFieldViewModeWhileEditing;
        _name.placeholder = @"请输入昵称:";
        _name.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth * 0.1, khieght * 0.05)];
        image.image = [UIImage imageNamed:@"zhanghu"];
        _name.leftView = image;

        
    }
    return _name;
}

-(NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"]) {
            _dataArr = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"] mutableCopy];
            NSLog(@"%@",_dataArr);
        }
//        else{
//            _dataArr = [NSMutableArray array];
//        }
    }
    return _dataArr;
}

@end
